<template>
  <div>
    <el-dialog
      :before-close="handleClose"
      :title="status === 'Add' ? '新增商品' : '编辑商品'"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        class="demo-ruleForm"
        label-width="180px"
      >
        <el-form-item label="特权名称" prop="name">
          <el-input v-model="ruleForm.name" placeholder="请输入特权名称" style="width: 60%;"></el-input>
        </el-form-item>
        <el-form-item label="特权图标" prop="icon_img">
          <el-upload
            :http-request="customizeUpload"
            :on-success="handleSuccess"
            :show-file-list="false"
            action=""
            class="avatar-uploader"

          >
            <img v-if="ruleForm.icon_img" :src="ruleForm.icon_img" class="avatar"/>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="特权图标暗" prop="dark_icon_img">
          <el-upload
            :http-request="customizeUpload"
            :on-success="handleSuccess1"
            :show-file-list="false"
            action=""
            class="avatar-uploader"

          >
            <img v-if="ruleForm.dark_icon_img" :src="ruleForm.dark_icon_img" class="avatar"/>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="特权图标说明图" prop="explain_img">
          <el-upload
            :http-request="customizeUpload"
            :on-success="handleSuccess2"
            :show-file-list="false"
            action=""
            class="avatar-uploader"

          >
            <img v-if="ruleForm.explain_img" :src="ruleForm.explain_img" class="avatar"/>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-input v-model="ruleForm.type" placeholder="请输入类型" style="width: 60%;" type="number"></el-input>
        </el-form-item>
        <el-form-item label="特权说明" prop="explain1">
          <el-input v-model="ruleForm.explain1" placeholder="请输入特权说明" style="width: 60%;"></el-input>
        </el-form-item>
        <el-form-item label="特权说明2" prop="explain2">
          <el-input v-model="ruleForm.explain2" placeholder="请输入特权说明2" style="width: 60%;"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input v-model="ruleForm.sort" placeholder="请输入排序" style="width: 60%;"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button
          :loading="submitLoading"
          type="primary"
          @click="submitForm('ruleForm')"
        >提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import { addPrivilege, editPrivilege } from '@/api/dailyCheck/memberPrivilegeManagement'

export default {
  name: 'addEditModal',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    // 模态框的状态 Add 为新增  Edit为编辑
    status: {
      type: String,
      default: ['Add']
    },
    detail: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      submitLoading: false,
      ruleForm: {
        name: null, // 特权名称
        status: null, // 状态 0禁用 1启用，必传
        type: null, // 特权类型，必传
        icon_img: null, // 特权图标，必传
        dark_icon_img: null, // 特权图标暗，必传
        explain_img: null, // 特权说明图，必传
        explain1: null, // 特权说明文字1，必传
        explain2: null, // 特权说明文字2，必传
        sort: null      // 排序
      },

      rules: {
        name: [{ required: true, message: '请选择特权名称', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'blur' }],
        type: [{ required: true, message: '请输入特权类型', trigger: 'blur' }],
        icon_img: [{ required: true, message: '请上传展示特权图标', trigger: 'blur' }],
        dark_icon_img: [{ required: true, message: '请上传展示特权图标暗', trigger: 'blur' }],
        explain_img: [{ required: true, message: '请上传展示特权说明图', trigger: 'blur' }],
        explain1: [{ required: true, message: '请输入特权说明文字1', trigger: 'blur' }],
        explain2: [{ required: true, message: '请输入特权说明文字2', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleClose(done) {
      this.$emit('update:dialogVisible', false)
      for (const detailKey in this.ruleForm) {
        this.ruleForm[detailKey] = null
      }
      this.resetForm('ruleForm')
      this.$emit('update:status', 'Add')
      this.$emit('update:detail', {})
    },
    submitForm(formName) {
      let data = {}
      for (const dataKey in this.ruleForm) {
        if (
          dataKey !== 'checkedCities' ||
          !(
            !this.ruleForm[dataKey] &&
            typeof this.ruleForm[dataKey] != 'undefined' &&
            this.ruleForm[dataKey] != 0
          )
        ) {
          data[dataKey] = this.ruleForm[dataKey]
        }
        // 过滤掉为 null
      }
      this.$refs[formName].validate(async valid => {
        if (!valid) {
          this.submitLoading = false
          return false
        }
        this.submitLoading = true
        let detail
        // 新增活动商品
        if (this.status === 'Add') {
          detail = await addPrivilege(data)
        } else {
          // 编辑
          detail = await editPrivilege(data)
        }
        if (detail.code === 200) {
          this.$message.success(this.status === 'Add' ? '添加成功!' : '编辑成功')
          this.$emit('update:dialogVisible', false)
          this.$emit('updateTable')
          for (const detailKey in this.ruleForm) {
            this.ruleForm[detailKey] = null
          }
          this.$emit('update:status', 'Add')
        }
        this.submitLoading = false
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    handleSuccess() {
      this.ruleForm.icon_img = this.ossLastImgUrl
    },
    handleSuccess1() {
      this.ruleForm.dark_icon_img = this.ossLastImgUrl
    },
    handleSuccess2() {
      this.ruleForm.explain_img = this.ossLastImgUrl
    }
  },
  watch: {
    async status(newVal) {
      /* 当 status 为 Edit 则 为编辑框, 需要完成数据的回显   */
      if (newVal == 'Edit') {
        this.ruleForm = Object.assign({}, this.detail)
      } else {
        for (const detailKey in this.ruleForm) {
          this.ruleForm[detailKey] = null
        }
      }
    }
  }
}
</script>

<style scoped>
::v-deep.avatar-uploader .el-upload {
  border        : 1px dashed #d9d9d9;
  border-radius : 6px;
  cursor        : pointer;
  position      : relative;
  overflow      : hidden;
}

.avatar-uploader .el-upload:hover {
  border-color : #409eff;
}

.avatar-uploader-icon {
  font-size   : 28px;
  color       : #8c939d;
  width       : 100px;
  height      : 100px;
  line-height : 100px;
  text-align  : center;
}

.avatar {
  width   : 100px;
  height  : 100px;
  display : block;
}

::v-deep .lu-input {
  width : 300px !important;
}
</style>
